//
//  NetWorkBaseApiEnvironmentManager.swift
//  app
//
//  Created by gy on 2023/7/28.
//

import Foundation

// 请求回调
typealias NetworkRequestHandle = ((DialogMessage) -> Void)

public enum NetWorkEnvironment {
    
    case dev
    case production
    case local
    
    public var baseUrl: URL {
        switch self {
        case .dev:
            return URL(string: "https://api-dev.jc-tour.net/")!
        case .production:
            return URL(string: "https://api-prod.jc-tour.net/")!
        case .local:
            return URL(string: "https://api-dev.jc-tour.net/")!
        }
    }

}

public let kApiEnvironmentManager = ApiEnvironmentManager.shared
open class ApiEnvironmentManager {
    
    public static let shared = ApiEnvironmentManager()
        
//#if DEBUG
//    public var environmentStr = NetWorkEnvironment.dev.baseUrl
//#else
    public var environmentStr = NetWorkEnvironment.production.baseUrl
//#endif
    
    public func readLocal() {
        
#if DEBUG
        if let urlString = UserDefaults.standard.value(forKey: "ApiEnvironmentKey") as? String, let url = URL(string: urlString) {
            environmentStr = url
        }
#endif
        
    }
    public func getShowTitle() -> String {
        
        if environmentStr.description == NetWorkEnvironment.production.baseUrl.description {
            return "生产"
        } else if environmentStr.description == NetWorkEnvironment.local.baseUrl.description {
            return "本地"
        }
        return "开发"
    }
    
    public func setNetWorkEnvironment(title: String) {
        
        var environmentText = environmentStr
        if title == "开发" {
            environmentText = NetWorkEnvironment.dev.baseUrl
        } else if title == "生产" {
            environmentText = NetWorkEnvironment.production.baseUrl
        } else if title == "local" {
            environmentText = NetWorkEnvironment.local.baseUrl
        }
        
        guard self.environmentStr != environmentText else { return }
        self.environmentStr = environmentText
        UserDefaults.standard.setValue(self.environmentStr.description, forKey: "ApiEnvironmentKey")
    }
    
}





